﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Intellisense;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace IntellisenseTest
{
    [TestClass]
    public class MSBuildMatchTest
    {
        #region GetMatches
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetMatchesTestMethod1()
        {
            MSBuildMatch.GetMatches(null, "aa", 0);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetMatchesTestMethod2()
        {
            MSBuildMatch.GetMatches("", "aa", 0);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetMatchesTestMethod3()
        {
            MSBuildMatch.GetMatches("aa", null, 0);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetMatchesTestMethod4()
        {
            MSBuildMatch.GetMatches("aa", "", 0);
        }

        [TestMethod]
        public void GetMatchesTestMethod5()
        {
            MatchCollection ms = MSBuildMatch.GetMatches("aiueo", "ie", 0);
            Assert.AreEqual(0, ms.Count);
        }

        [TestMethod]
        public void GetMatchesTestMethod6()
        {
            MatchCollection ms = MSBuildMatch.GetMatches("aiueo", "ue", 0);
            Assert.AreEqual(1, ms.Count);
            Assert.AreEqual("ue", ms[0].Value);
            Assert.AreEqual(2, ms[0].Index);
        }

        [TestMethod]
        public void GetMatchesTestMethod7()
        {
            MatchCollection ms = MSBuildMatch.GetMatches("aiueoue", "ue", 0);
            Assert.AreEqual(2, ms.Count);
            Assert.AreEqual("ue", ms[0].Value);
            Assert.AreEqual(2, ms[0].Index);
            Assert.AreEqual("ue", ms[1].Value);
            Assert.AreEqual(5, ms[1].Index);

        }

        #endregion

        #region FindLastMatch
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FindLastMatchTestMethod1()
        {
            MSBuildMatch.FindLastMatch("", "a", 0,false);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FindLastMatchTestMethod2()
        {
            MSBuildMatch.FindLastMatch(null, "a", 0, false);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FindLastMatchTestMethod3()
        {
            MSBuildMatch.FindLastMatch("a", "", 0, false);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FindLastMatchTestMethod4()
        {
            MSBuildMatch.FindLastMatch("a", null, 0, false);
        }

        [TestMethod]
        public void FindLastMatchTestMethod5()
        {
            Match m = MSBuildMatch.FindLastMatch("abcde", "z", 0, false);
            Assert.IsNull(m);
        }

        [TestMethod]
        public void FindLastMatchTestMethod6()
        {
            Match m = MSBuildMatch.FindLastMatch("abcde", "cd", 2, false);
            Assert.IsNull(m);
        }

        [TestMethod]
        public void FindLastMatchTestMethod7()
        {
            Match m = MSBuildMatch.FindLastMatch("abcde", "cd", 4, false);
            Assert.IsNull(m);
        }

        [TestMethod]
        public void FindLastMatchTestMethod8()
        {
            Match m = MSBuildMatch.FindLastMatch("abcde", "cd", 3, false);
            Assert.IsNotNull(m);
            Assert.AreEqual(true, m.Success);
        }

        #endregion

        #region IsSelectedBetweenLastMatch
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void IsSelectedBetweenLastMatchTestMethod1()
        {
            MSBuildMatch.IsSelectedBetweenLastMatch("", "a", 0);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void IsSelectedBetweenLastMatchTestMethod2()
        {
            MSBuildMatch.IsSelectedBetweenLastMatch(null, "a", 0);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void IsSelectedBetweenLastMatchTestMethod3()
        {
            MSBuildMatch.IsSelectedBetweenLastMatch("a", "", 0);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void IsSelectedBetweenLastMatchTestMethod4()
        {
            MSBuildMatch.IsSelectedBetweenLastMatch("a", null, 0);
        }

        [TestMethod]
        public void IsSelectedBetweenLastMatchTestMethod5()
        {
            bool result = MSBuildMatch.IsSelectedBetweenLastMatch("a", "b", 0);
            Assert.IsFalse(result);
        }

        [TestMethod]
        public void IsSelectedBetweenLastMatchTestMethod6()
        {
            bool result = MSBuildMatch.IsSelectedBetweenLastMatch("abcde", "cd", 0);
            Assert.IsFalse(result);
        }

        [TestMethod]
        public void IsSelectedBetweenLastMatchTestMethod7()
        {
            bool result = MSBuildMatch.IsSelectedBetweenLastMatch("abcde", "cd", 3);
            Assert.IsTrue(result);
        }

        #endregion

        #region IsExist
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void IsExistTestMethod1()
        {
            MSBuildMatch.IsExist("", "a");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void IsExistTestMethod2()
        {
            MSBuildMatch.IsExist(null, "a");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void IsExistTestMethod3()
        {
            MSBuildMatch.IsExist("a", "");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void IsExistTestMethod4()
        {
            MSBuildMatch.IsExist("a", null);
        }

        [TestMethod]
        public void IsExistTestMethod5()
        {
            bool result = MSBuildMatch.IsExist("abcde", "c");
            Assert.IsTrue(result);
        }

        [TestMethod]
        public void IsExistTestMethod6()
        {
            bool result = MSBuildMatch.IsExist("abcde", "z");
            Assert.IsFalse(result);
        }

        #endregion

        #region GetMatchedSpans
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetMatchedSpansTestMethod1()
        {
            MSBuildMatch.GetMatchedSpans("", "a");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetMatchedSpansTestMethod2()
        {
            MSBuildMatch.GetMatchedSpans(null, "a");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetMatchedSpansTestMethod3()
        {
            MSBuildMatch.GetMatchedSpans("a", "");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetMatchedSpansTestMethod4()
        {
            MSBuildMatch.GetMatchedSpans("a", null);
        }

        [TestMethod]
        public void GetMatchedSpansTestMethod5()
        {
            List<TextSpan> result = MSBuildMatch.GetMatchedSpans("a", "z");
            Assert.IsNull(result);
        }

        [TestMethod]
        public void GetMatchedSpansTestMethod6()
        {
            List<TextSpan> result = MSBuildMatch.GetMatchedSpans("abcde", "c");
            Assert.AreEqual(1,result.Count);
            Assert.AreEqual(2,result[0].StartPosition);
            Assert.AreEqual(3, result[0].EndPosition);
        }

        [TestMethod]
        public void GetMatchedSpansTestMethod7()
        {
            List<TextSpan> result = MSBuildMatch.GetMatchedSpans("abcdec", "c");
            Assert.AreEqual(2, result.Count);
            Assert.AreEqual(2, result[0].StartPosition);
            Assert.AreEqual(3, result[0].EndPosition);
            Assert.AreEqual(5, result[1].StartPosition);
            Assert.AreEqual(6, result[1].EndPosition);
        }

        #endregion

    }
}
